﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace SerialPortTest
{
    public class Serial_cfg
    {
        [XmlIgnore]
        public string PortName { get { return string.Format("COM{0}", (int)PortID); } }
        [Display(Name = "通讯口")] [XmlAttribute("Port")] public SerialPortEnum PortID { get; set; }
        [Display(Name = "波特率")] [XmlAttribute] public SerialBoundRate BaudRate { get; set; }
        [Display(Name = "数据位")] [XmlAttribute] public SerialDataBits DataBits { get; set; }
        [Display(Name = "停止位")] [XmlAttribute] public StopBits StopBits { get; set; }
        [Display(Name = "校验位")] [XmlAttribute] public System.IO.Ports.Parity Party { get; set; }

        public Serial_cfg()
        {
            this.PortID = SerialPortEnum.com1;
            this.BaudRate = SerialBoundRate.BR9600;
            this.StopBits = StopBits.One;
            this.DataBits = SerialDataBits.DB8;

            this.Party = Parity.None; //奇偶校验
        }

        public override string ToString()
        {
            return $"{PortName} {BaudRate} {DataBits} {StopBits} {Party}";
        }

        public interface ISerialRecv
        {
            void On_serial_recved(byte[] buff, Serial_cfg cfg);
        }
    }
}
